#!/bin/bash
#It's will to be used to install docker、middleware................
#author:wqf    time:2022/08/22
#！！！注意所有离线安装包放在/opt/下！！！
read -p "please input your choice:
0-install-docker: docker_version20.10.9，按需更改安装目录
1-install-redis: 按需更改安装目录
2-install-rocketmq: 按需更改安装目录
3-install-postgres: 按需更改安装目录
4-install-clickhouse: 按需更改安装目录
5-lvm-extend: 按需更改扩容挂载目录
": name

function 0-install-docker () {

    #卸载旧版docker,清理环境
    yum remove  -y docker \
    docker-client docker-client-latest \
    docker-common docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-selinux \
    docker-engine-selinux \
    docker-engine
    ping -c 3 mirrors.aliyun.com 
    if [ $? -eq 0 ];then    
        #安装docker及其依赖
        yum install -y yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        yum install -y docker-ce-20.10.9
        mkdir /etc/docker
        echo "
 
        {
        
        "graph": "/data/docker",
        
        "registry-mirrors": ["http://registry:5000"],
        
        "insecure-registries": ["registry:5000"],
        
        "log-driver": "json-file",
        
        "log-opts": {
        
        "max-size": "100m",
        
        "max-file": "3"
        
        }
        
        }
        " > /etc/docker/daemon.json
        systemctl enable docker && systemctl start docker
    else
        #不通外网得安装
        echo "----------------------------下载好rpm包放在/opt/docker/下-------------------------------"
        cd /opt/docker/ && rpm -ivh *.rpm --force
        mkdir /etc/docker
        echo "
 
        {
        
        "graph": "/data/docker",
        
        "registry-mirrors": ["http://registry:5000"],
        
        "insecure-registries": ["registry:5000"],
        
        "log-driver": "json-file",
        
        "log-opts": {
        
        "max-size": "100m",
        
        "max-file": "3"
        
        }
        
        }
        " > /etc/docker/daemon.json
        systemctl enable docker && systemctl start docker
    fi
}
function 1-install-redis () {
    #下载redis
    mkdir -p /opt/redis/
    wget -P /opt/redis/ https://download.redis.io/releases/redis-6.2.6.tar.gz
    tar -zxvf /opt/redis/redis-6.2.6.tar.gz -C /data/
    #安装依赖
    yum -y install gcc gcc-c++
    	#编译安装
        cd /data/redis-6.2.6
    	make 
    	cd /data/redis-6.2.6/src/
    	make install
    	mkdir -p /data/redis-6.2.6/{etc,bin}
    	mv /data/redis-6.2.6/redis.conf /data/redis-6.2.6/etc/
    	cd /data/redis-6.2.6/src
    	mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel  redis-server redis-trib.rb  /data/redis-6.2.6/bin

	#修改redis配置文件
    	sed -i '75s/bind 127.0.0.1/bind 0.0.0.0/g' /data/redis-6.2.6/etc/redis.conf
    	sed -i 's/daemonize no/daemonize yes/g' /data/redis-6.2.6/etc/redis.conf
    	sed -i 's/appendonly no/appendonly yes/g' /data/redis-6.2.6/etc/redis.conf
    	sed -i '902s/^/requirepass Iot@123/g' /data/redis-6.2.6/etc/redis.conf
    	
	#启动redis
	/data/redis-6.2.6/bin/redis-server   /data/redis-6.2.6/etc/redis.conf  
	
	#redis加入开机自启动
	echo "/data/redis-6.2.6/bin/redis-server   /data/redis-6.2.6/etc/redis.conf" >> /etc/rc.d/rc.local
	ps -ef | grep redis    
}

function 2-install-rocketmq () {
    read -r -p "采用docker-compose安装rocketmq ，请确认[Y/n] ": input_rocketmq
    case ${input_rocketmq} in
    [yY][eE][sS]|[yY])
        docker info 
        if [ $? -eq 0 ];then
            echo "-----------------------docker已安装------------------------"
            sudo curl -L https://get.daocloud.io/docker/compose/releases/download/v2.6.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
            chmod +x /usr/local/bin/docker-compose
            docker-compose version
            if [ $? -eq 0 ];then 
                mkdir -p /data/rocketmq/{namesrv,broker}
                mkdir -p /data/rocketmq/namesrv/{logs,store}
                mkdir -p /data/rocketmq/broker/{logs,store}
                echo """
version: '2'
services:
  namesrv:
    image: rocketmqinc/rocketmq
    container_name: rmqnamesrv
    restart: always
    ports:
      - 9876:9876
    volumes:
      - /data/rocketmq/namesrv/logs:/home/rocketmq/logs
      - /data/rocketmq/namesrv/store:/home/rocketmq/store
    command: sh mqnamesrv
  broker:
    image: rocketmqinc/rocketmq
    container_name: rmqbroker
    restart: always
    ports:
      - 10909:10909
      - 10911:10911
      - 10912:10912
    volumes:
      - /data/rocketmq/broker/logs:/root/logs
      - /data/rocketmq/broker/store:/root/store
      - ./broker.conf:/opt/rocketmq-4.4.0/conf/broker.conf
    command: sh mqbroker -n namesrv:9876 -c /opt/rocketmq-4.4.0/conf/broker.conf
    depends_on:
      - namesrv
    environment:
      - JAVA_HOME=/usr/lib/jvm/jre
  console:
    image: styletang/rocketmq-console-ng
    container_name: rocketmq-console-ng
    restart: always
    ports:
      - 31003:8080
    depends_on:
      - namesrv
    environment:
      - JAVA_OPTS= -Dlogging.level.root=info   -Drocketmq.namesrv.addr=rmqnamesrv:9876
      - Dcom.rocketmq.sendMessageWithVIPChannel=false
        """ >> /data/rocketmq/doker-compose.yml
                docker-compose up -d 
                sleep 20s
                dcoker ps -a | grep rocketmq
                echo "---------------------检查rocketmq容器是否正常启动-----------------------------------"
            else
                echo "---------------------先安装docker-compose-------------------------------"
            fi

        else
            echo "------------------------------------请先使用此脚本安装docker-----------------------------"
        fi
        ;;
    [nN][oO]|[nN])
        echo "-------------------------------不使用此方式安装，已退出--------------------------------"
        exit 1
        ;;
    *)
        echo "----------------------------------Invalid input---------------------------------------"
        exit 1
        ;;
    esac
    
}

function 3-install-postgres () {

}

function 4-install-clickhouse () {

}

function 5-lvm-extend () {
    lsblk
    read -r -p "请确认磁盘挂载情况,yes已挂载；no未挂载，自动挂载；Are You Sure? [Y/n] " input
    case $input in
    [yY][eE][sS]|[yY])
        echo "----------------------------------Yes已挂载-----------------------------------"
        ;;
 
    [nN][oO]|[nN])
        echo "----------------------------------No未挂载------------------------------------"
        disk=`lsblk | grep disk | tail -1 | awk '{print $1}'`
        pvcreate /dev/$disk
        vgcreate data_vg  /dev/$disk
        lvcreate  -n data_lv -l +100%FREE data_vg
        mkdir /data
        mkfs.xfs  /dev/data_vg/data_lv
        echo '/dev/data_vg/data_lv /data xfs defaults 0 0' >> /etc/fstab
        mount  -a
        df -h
        lsblk
        echo "------------------若出现多个磁盘，请手动扩容其余磁盘空间至/data/目录下----------"
    *)
        echo "Invalid input..."
        exit 1
        ;;
    esac
}

case $name in 
0-install-docker)
    0-install-docker
    ;;
1-install-redis)
    1-install-redis
    ;;
2-install-rocketmq)
    2-install-rocketmq
    ;;
3-install-postgres)
    3-install-postgres
    ;;
4-install-clickhouse)
    4-install-clickhouse
    ;;
5-lvm-extend)
    5-lvm-extend
    ;;
*)
	echo "----------------------------------------------------no such choice-----------------------------------------"
    exit 1
    ;;
esac